In [2]:
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.io as pio

pio.renderers.default = "iframe"

import warnings
# Ignore all warnings
warnings.filterwarnings("ignore", category=FutureWarning)

def make_graph(stock_data, revenue_data, stock):
    fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
    stock_data_specific = stock_data[stock_data.Date <= '2021-06-14']
    revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
    fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
    fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
    fig.update_xaxes(title_text="Date", row=1, col=1)
    fig.update_xaxes(title_text="Date", row=2, col=1)
    fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
    fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
    
    fig.update_layout(showlegend=False,
    height=900,
    title=stock,
    xaxis_rangeslider_visible=True)
    fig.show()
    from IPython.display import display, HTML
    fig_html = fig.to_html()
    display(HTML(fig_html))
    fig.write_html("my_plot.html")
    print("Graph saved! Open my_plot.html in your browser.")

###QUESTION 1

tesla = yf.Ticker("TSLA")

tesla_data = tesla.history(period="max")

tesla_data.reset_index(inplace=True)


###QUESTION 2

url = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm'

html_data = requests.get(url).text

soup = BeautifulSoup(html_data, 'html5lib')

data = pd.read_html(url)

tesla_revenue = data[1]

tesla_revenue = tesla_revenue.rename(columns={'Tesla Quarterly Revenue (Millions of US $)':'Date'})
tesla_revenue = tesla_revenue.rename(columns={'Tesla Quarterly Revenue (Millions of US $).1':'Revenue'})

tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',|\$',"",regex=True)

tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]


###QUESTION 3

gme = yf.Ticker("GME")
gme_data = gme.history(period ="max")
gme_data.reset_index(inplace=True)


###QUESTION 4

url_2 = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html'

html_data_2 = requests.get(url_2).text

soup_2 = BeautifulSoup(html_data_2, 'html5lib')

data_2 = pd.read_html(url_2)

gme_revenue = data_2[1]
gme_revenue = gme_revenue.rename(columns = {'GameStop Quarterly Revenue (Millions of US $)': 'Date'})
gme_revenue = gme_revenue.rename(columns = {'GameStop Quarterly Revenue (Millions of US $).1': 'Revenue'})

gme_revenue["Revenue"] = gme_revenue['Revenue'].str.replace(',|\$',"",regex=True)
gme_revenue.dropna(inplace=True)
gme_revenue = gme_revenue[gme_revenue['Revenue'] != ""]


###QUESTION 5

make_graph(tesla_data, tesla_revenue,'Tesla')
make_graph(gme_data, gme_revenue,'GME')
<>:61: SyntaxWarning:

invalid escape sequence '\$'

<>:88: SyntaxWarning:

invalid escape sequence '\$'

<>:61: SyntaxWarning:

invalid escape sequence '\$'

<>:88: SyntaxWarning:

invalid escape sequence '\$'

/var/folders/sb/gxq_6d3d3tn326ty5q_fhqv00000gn/T/ipykernel_63882/1138238388.py:61: SyntaxWarning:

invalid escape sequence '\$'

/var/folders/sb/gxq_6d3d3tn326ty5q_fhqv00000gn/T/ipykernel_63882/1138238388.py:88: SyntaxWarning:

invalid escape sequence '\$'

Graph saved! Open my_plot.html in your browser.
Graph saved! Open my_plot.html in your browser.